package com.zz.collection.doudizhu;

import java.util.*;

public class Room {
    //必须有一副牌
    private List<Card> allCards = new ArrayList <>();
    public Room(){
        //1.做出54张牌，存入到集合allCards
        //a、点数：个数确定了，类型确定、
        String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        //b.花色，个数确定了，类型确定
        String[] colors = {"♣", "❤","♠","◇"};
        int size = 0;//表示每张牌的大小
        //c.遍历，再遍历花色，组织牌
        for(String number: numbers){
            //number = "3"
            size++;
            for (String color : colors){
                //得到一张牌
                Card c = new Card(number, color, size);
                allCards.add(c);
            }
        }
        //单独存入大小王
        Card c1 = new Card("", "小王", ++size);
        Card c2 = new Card("", "大王", ++size);
        Collections.addAll(allCards, c1, c2);//批量加数据
        System.out.println("这是一副新牌:" + allCards);
    }

    public void start() {
        //1.洗牌
        Collections.shuffle(allCards);//打乱顺序
        System.out.println("洗牌后：" + allCards);

        //2.发牌，首先肯定要定义 三个玩家，List(可重复ArrayList，有索引） Set（TreeSet 不重复，无索引)
        List<Card> Tom = new ArrayList <>();
        List<Card> Marry = new ArrayList <>();
        List<Card> Jack = new ArrayList <>();

        //正式发牌给这三个玩家，依次发出51张牌，剩余三张作为底牌
        for(int i = 0; i < allCards.size() - 3; i++){
            Card c = allCards.get(i);
            //判断牌发给谁
            if(i % 3 == 0){
                //Tom
                Tom.add(c);
            }else if(i % 3 == 1){
                Marry.add(c);
            }else
                Jack.add(c);
        }

        //3.对玩家的牌进行排序
        sortCards(Tom);
        sortCards(Marry);
        sortCards(Jack);
        //4.看牌
        System.out.println("Tom" + Tom);
        System.out.println("Marry" + Marry);
        System.out.println("Jack" + Jack);
        //截取指定位置的元素，包前不包后
        List<Card> last = allCards.subList(allCards.size() - 3, allCards.size());//51, 52, 53
        System.out.println("last" + last);
        Tom.addAll(last);//添加所有元素
        sortCards(Tom);
        System.out.println("Tom抢到地主之后：" + Tom);
    }

    private void sortCards(List<Card> cards) {
        Collections.sort(cards, new Comparator <Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return o2.getSize() - o1.getSize();
            }
        });
    }
}
